 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
</head>

<body>


<?PHP



$con=mysqli_connect("localhost","root","","bd_news");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}


	$title = mysqli_real_escape_string($con, $_POST['title']);
	$description = mysqli_real_escape_string($con, $_POST['description']);
	$maintext = mysqli_real_escape_string($con, $_POST['maintext']);

	$date = date("Y-m-d");
	
	$id_user = mysqli_real_escape_string($con, $_POST['id_user']);
	
	$tag1 = mysqli_real_escape_string($con, $_POST['TAG1']);
	$tag2 = mysqli_real_escape_string($con, $_POST['TAG2']);
	$tag3 = mysqli_real_escape_string($con, $_POST['TAG3']);
	$tag4 = mysqli_real_escape_string($con, $_POST['TAG4']);
	

	

$sql="INSERT INTO news (`ID_NEWS`, `TITLE`, `DESCRIPTION`, `MAINTEXT`, `DATE`, `RATING`, `ID_USER`) VALUES (NULL, '$title', '$description', '$maintext', '$date', NULL, '$id_user');";

if (!mysqli_query($con,$sql)) {
  die('Error: ' . mysqli_error($con));
}
$id_inserted = mysqli_insert_id($con);

if($tag1 != ''){
//INSERT INTO `bd_news`.`news_tags` (`ID_NEWS`, `ID_TAGS`) VALUES ('13', '1');
	$result = mysqli_query($con,"INSERT INTO `news_tags` (`ID_NEWS`, `ID_TAGS`) VALUES ('$id_inserted', '$tag1');");
	//echo 'tag 1 to: '.$tag1.'<br>';
	}
if($tag2 != ''){
	$result = mysqli_query($con,"INSERT INTO `news_tags` (`ID_NEWS`, `ID_TAGS`) VALUES ('$id_inserted', '$tag2');");
	//echo 'tag 2 to: '.$tag2.'<br>';
	}
if($tag3 != ''){
	$result = mysqli_query($con,"INSERT INTO `news_tags` (`ID_NEWS`, `ID_TAGS`) VALUES ('$id_inserted', '$tag3');");
	//echo 'tag 3 to: '.$tag3.'<br>';
	}
if($tag4 != ''){
	$result = mysqli_query($con,"INSERT INTO `news_tags` (`ID_NEWS`, `ID_TAGS`) VALUES ('$id_inserted', '$tag4');");
	//echo 'tag 4 to: '.$tag4.'<br>';
	}




mysqli_close($con);	
echo "<meta http-equiv='refresh' content='0;url=./newsmyadm.php'>";	
?>

</body>

</html>
